#include <iostream>

#include "base/object_pool.h"

using namespace std;
using namespace base;

class IntPool : public ObjectPool<int> {
 public:
  virtual int * Create(const std::string & key) {
    return new int(0);
  }
  virtual void Destroy(int * p) {
    return delete p;
  }
};


void * ThreadFun(void * data) {
  ObjectPool<int> * pool = (IntPool *)data;

  int * p = pool->Alloc(string());
  for (int i = 0; i < 10000000; ++i) {
    pool->Release(string(), p, true);
    p = pool->Alloc(string());
  }
  return NULL;
}

const int kThreadCount = 20;

int main() {
  LOG_INIT("./log", "WARN");
  IntPool pool;

  pthread_t ts[kThreadCount];

  for (int i = 0; i < kThreadCount; ++i) {
    pthread_create(&ts[i], NULL, ThreadFun, &pool);
  }

  for (int i = 0; i < kThreadCount; ++i) {
    pthread_join(ts[i], NULL);
  }
  return 0;
}

